#pragma once

#ifdef LIBCHATSIO_EXPORTS
#define LIBCHATSIO_API __declspec(dllexport)
#else
#define LIBCHATSIO_API __declspec(dllimport)
#endif

#include "sio_client.h"
#include "apThread.h"
#include "apSocketListener.h"

#include <functional>
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <string>
#include <list>

#ifdef WIN32
#define HIGHLIGHT(__O__) std::cout<<__O__<<std::endl
#define EM(__O__) std::cout<<__O__<<std::endl
#endif

#include <stdio.h>
#include <tchar.h>

using namespace sio;
using namespace std;

class apSocketIO : public apThread
{
public:
	apSocketIO(std::string &username, std::string &room_id);
	~apSocketIO();

	void setListener(SocketEventListener *lis) {
		m_listener = lis;
	}

	void on_connected();

	void on_close(client::close_reason const& reason);

	void on_fail();

	void on_reconnecting();

	void on_reconnect();

	void on_ack();

	void connect(const char* url);

	void login();

	void join();

	void leave();

	void send_message(const char* to, const char* msg);

	void send_raw_message(const char* to, const char* raw_msg);

	void logout();

	void disconnect();
protected:
	virtual void thread_proc();
private:
	void bind_events();

	void close();
private:
	SocketEventListener*m_listener;

	sio::client			m_client;
	socket::ptr			m_current_socket;
	int					m_participants;

	bool				m_connected;

	std::mutex						_lock;
	std::condition_variable_any		_cond;

	std::string			m_username;
	std::string			m_room;
};

